﻿using System;
using System.Collections.Generic;
using System.Text;
using Model;
using GameHelp;
using System.Threading;
using Common;

namespace helper
{
    public partial class AutoEngine
    {
        /// <summary>
        /// 将数据发送到服务端
        /// </summary>
        /// <param name="datas"></param>
        private void Forward(byte[] datas)
        {
            Forward(datas, datas.Length);
        }

        /// <summary>
        /// 检测30困难副本是否通关
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        private bool FightRadio30Sucess(byte[] datas, int length)
        {
            string s = UTF8Encoding.UTF8.GetString(datas, 0, length);
            if (s.IndexOf(account.Character.Name) > -1)
            {
                if (s.IndexOf("焚骨窟") > -1 && s.IndexOf("获得了") > -1)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 检测50苦难副本是否通关
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        private bool FightRadio50Sucess(byte[] datas, int length)
        {
            string s = UTF8Encoding.UTF8.GetString(datas, 0, length);
            if (s.IndexOf(account.Character.Name) > -1)
            {
                if (s.IndexOf("天人福邸") > -1 && s.IndexOf("获得了") > -1)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 检测70困难副本是否通关
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        private bool FightRadio70Sucess(byte[] datas, int length)
        {
            string s = UTF8Encoding.UTF8.GetString(datas, 0, length);
            if (s.IndexOf(account.Character.Name) > -1)
            {
                if (s.IndexOf("困仙阵") > -1 && s.IndexOf("第44层获得了") > -1)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 将数据转发到指定的连接，一般用于不期望响应的操作，比如某角色说话的信息
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="length"></param>
        /// <param name="client"></param>
        /// <returns>返回是否转发成功</returns>
        public bool Forward(byte[] datas, int length)
        {
            try
            {
                this.serverClient.GetStream().Write(datas, 0, length);
            }
            catch (Exception ex)
            {
                //调试信息记录
                Tools.PrintException(ex);
                return false;
            }
            return true;
        }

        /// <summary>
        /// 结束战斗
        /// </summary>
        /// <returns></returns>
        public bool FinshFight()
        {
            Forward(Operationer.FightCompleteCode, Operationer.FightCompleteCode.Length);
            return true;
        }

        /// <summary>
        /// 接受30困难副本任务
        /// </summary>
        /// <returns></returns>
        public bool QuestacceptRadio30()
        {
            MessageWrite("接受30困难副本任务...");
            Forward(Operationer.QuestacceptRadio30ACode);
            Thread.Sleep(1000);
            Forward(Operationer.QuestacceptRadio30BCode);
            Thread.Sleep(1000);
            Forward(Operationer.QuestacceptRadio30CCode);
            return true;
        }

        /// <summary>
        /// 提交30困难副本任务
        /// </summary>
        /// <returns></returns>
        public bool FinshQuestRadio30()
        {
            MessageWrite("提交30困难副本任务...");
            Forward(Operationer.FinshQuestRadio30ACode);
            Thread.Sleep(1000);
            Forward(Operationer.FinshQuestRadio30BCode);
            Thread.Sleep(1000);
            Forward(Operationer.FinshQuestRadio30CCode);
            return true;
        }

        /// <summary>
        /// 进入30副本
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public bool EnterRadio30(RadioType type)
        {
            if (type == RadioType.Hard)
            {
                Forward(Operationer.RadioHard30EnterCode);
            }
            else
            {
                Forward(Operationer.RadioEasy30EnterCode);
            }
            return true;
        }

        /// <summary>
        /// 30副本进入下一层战斗
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public bool FightRadio30(RadioType type)
        {
            if (type == RadioType.Hard)
            {
                Forward(Operationer.RadioHard30NextFightCode);
            }
            else
            {
                Forward(Operationer.RadioEasy30NextFightCode);
            }
            return true;
        }

        /// <summary>
        /// 返回可以进行下次战斗的计时，单位秒
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static int GetNextFightTime(byte[] datas, int length)
        {
            if (length > 7)
            {
                if (datas[0] == 0x09 && datas[1] == 0x00 && datas[2] == 0xff && datas[3] == 0x0f
                    && datas[4] == 0x00 && datas[5] == 0xf3 && datas[6] == 0x07)
                {
                    return (int)datas[9];
                }
            }
            return -1;
        }
    }
}
